﻿package Naruto {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.media.Sound;
	import flash.media.SoundChannel;
    import flash.net.URLRequest;
    import flash.system.System;
	
	import Naruto.Sound.*;
	
    public class PlaySound extends Sprite {
        private var _mySound:Sound;
		private var _soundChannel:SoundChannel;
		private var _request:URLRequest;
		
		/**
		 * 声音加载完成事件监听列表
		 * 
		 */
		private var _soundLoadCompleteListeners : Array = null;
		
		public function PlaySound(num:int){
			this._mySound = new Sound();
			this._soundChannel = new SoundChannel();
            var url:String = "Naruto/Sound/S" + num + ".mp3";
			this._request = new URLRequest(url);
			_mySound.addEventListener(Event.COMPLETE, this.soundLoadComplete);
			_mySound.load(this._request);
		}
		
		public function soundLoadComplete(e : Event) : void {
			this.onSoundLoadComplete(e);
		}
		
		/**
		 * 声音加载完成事件函数
		 *
		 * @param e
		 * 
		 */
		protected function onSoundLoadComplete(e : Event) : void {
			if (this._soundLoadCompleteListeners != null) {
				for each (var obj:Function   in this._soundLoadCompleteListeners) {
					obj(e);
				}
			}
		}

		public function addSoundLoadCompleteListener(f : Function) : void {
			if (this._soundLoadCompleteListeners == null) {
				this._soundLoadCompleteListeners = new Array();
			}

			this._soundLoadCompleteListeners.push(f);
		}
		
		public function Play(){
			//mySound.play(0,int.MAX_VALUE);
			this._soundChannel = _mySound.play();
		}
    }
}